<template>
  <view>
    <cu-custom bgColor="bg-gradual-blue" v-if="PageCur!='mine'">
      <view slot="content">Ceart博客</view>
    </cu-custom>
    <home v-if="PageCur=='home'" :pageInex='pageInex' :userInfo='userInfo' ref="myhome" v-on:homePageInex="homePageInex"></home>
    <articles v-if="PageCur=='article'" :pageInex='pageInex' ref="myarticles" v-on:articlesPageInex="articlesPageInex"></articles>
    <diary v-if="PageCur=='diary'" :pageInex='pageInex' ref="mydiary"></diary>
    <mine v-if="PageCur=='mine'" :userInfo='userInfo' :hasUserInfo='hasUserInfo' v-on:childByValue="childByValue"></mine>

    <view class="cu-bar tabbar bg-white shadow foot">
      <view class="action" @click="NavChange" data-cur="home">
        <view class='cuIcon-cu-image'>
          <image :src="'/static/tabs/home' + [PageCur=='home'?'_cur':''] + '.png'"></image>
        </view>
        <view :class="PageCur=='home'?'text-blue':'text-gray'">首页</view>
      </view>
      <view class="action" @click="NavChange" data-cur="article">
        <view class='cuIcon-cu-image'>
          <image :src="'/static/tabs/article'+ [PageCur=='article'?'_cur':''] + '.png'"></image>
        </view>
        <view :class="PageCur=='article'?'text-blue':'text-gray'">分类</view>
      </view>
      <view class="action text-gray add-action">
        <button class="cu-btn cuIcon-scan bg-blue shadow" @click='goto'></button>
        扫一扫
      </view>
      <view class="action" @click="NavChange" data-cur="diary">
        <view class='cuIcon-cu-image'>
          <image :src="'/static/tabs/diary'+ [PageCur=='diary'?'_cur':''] + '.png'"></image>
        </view>
        <view :class="PageCur=='diary'?'text-blue':'text-gray'">时光轴</view>
      </view>
      <view class="action" @click="NavChange" data-cur="mine">
        <view class='cuIcon-cu-image'>
          <image :src="'/static/tabs/my'+ [PageCur=='mine'?'_cur':''] + '.png'"></image>
        </view>
        <view :class="PageCur=='mine'?'text-blue':'text-gray'">我的</view>
      </view>
    </view>
  </view>
</template>

<script>
	import api from '../../utils/api.js';
  export default {
    data() {
      return {
        pageInex:1,
        userInfo:{},
        hasUserInfo:false,
        PageCur: 'home',
        swiperList: []
      }
    },
		onLoad:async function(){
			var _this = this;
			uni.getSetting({
				success: res => {
					if (res.authSetting['scope.userInfo']) {
						new Promise(resolve => {
								resolve(api.getUserInfo());
						}).then(result => {
							if(result.result.data.length>0){
								// 已经授权，可以直接调用 getUserInfo 获取头像昵称
								_this.userInfo = result.result.data[0];
								_this.hasUserInfo = true;
								getApp().globalData.userInfo = result.result.data[0];
								getApp().globalData.hasUserInfo = true;
							}
						})			
					}
				}
			}) 
		},
    onShow(){
				this.updateUserInfo();
    },
    onPullDownRefresh() {//下拉刷新
      this.pageInex=1;
      this.PageCur=='home'&&this.$refs.myhome.getHomePostsList('below',this.pageInex);
      this.PageCur=='article'&&this.$refs.myarticles.getArticle('below',this.pageInex);
			this.PageCur=='diary'&&this.$refs.mydiary.getDiaryTab('below');
    },
    onReachBottom(){//上拉加载
      this.pageInex=this.pageInex+1;
      this.PageCur=='home'&&this.$refs.myhome.getHomePostsList('up',this.pageInex);
      this.PageCur=='article'&&this.$refs.myarticles.getArticle('up',this.pageInex);    
			this.PageCur=='diary'&&this.$refs.mydiary.getDiary('up',this.pageInex);
    },
    methods: {
      NavChange: function(e) {
        this.PageCur = e.currentTarget.dataset.cur;
        this.pageInex=1;
      },
      goto: function() {
        wx.cloud.callFunction({
          name: 'getImgList',
          data: {},
          success: res => {
           // console.log(res)
          }})
        wx.scanCode({
          success(res) {
            // console.log(res)
            wx.showModal({
              title: '扫码内容',
              content: res.result,
            })
          }
        })
      },
      homePageInex:function(pageInex){//第N页
        this.pageInex=parseInt(pageInex);
      },
      articlesPageInex:function(pageInex){//第N页
        this.pageInex=parseInt(pageInex);
      },
			childByValue:function(info){
        this.userInfo=info.userInfo;
				this.hasUserInfo=info.hasUserInfo;
      }
    },
		onShareAppMessage: function(res) {//调用API分享给好友
			return {
				title: getApp().globalData.blogName,
				imageUrl: getApp().globalData.imgUrl+getApp().globalData.logo,   //图片地址
				path: `/pages/index/index`,
				success: function success(res) {
					console.log("转发成功", res);
				},
				fail: function fail(res) {
					console.log("转发失败", res);
				}
			}
		}
  }
</script>

<style>
  .userinfo {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .userinfo-avatar {
    width: 128rpx;
    height: 128rpx;
    margin: 20rpx;
    border-radius: 50%;
  }

  .userinfo-nickname {
    color: #aaa;
  }

  .usermotto {
    margin-top: 200px;
  }
 
</style>
